package com.example.juc.until;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class Test{
    public synchronized void add(){
        add();
    }

    public static void main(String[] args) {
        Test test= new Test();
        test.add();
    }
}
class Ch{
    //线程间的定制化通信
    private static int flag=1;
    private Lock lock =new ReentrantLock();
    private Condition c1 =lock.newCondition();
    private Condition c2 =lock.newCondition();
    private Condition c3 =lock.newCondition();
    //写死的线程一对一绑定的Condition
    public void t5(int c) throws InterruptedException {
        lock.lock();
        try {
            while (flag!=1){
                c1.await();
            }
            for (int i = 1; i <=5 ; i++) {
                System.out.println(Thread.currentThread().getName()+"::"+i+":"+c+"次数");
            }
            flag=2;
            c2.signal();  //通知线程2
        }finally {
            lock.unlock();
        }
    }

    public void t10(int c) throws InterruptedException {
        lock.lock();
        try {
            while (flag!=2){
                c2.await();
            }
            flag=3;
            for (int i = 1; i <=10 ; i++) {
                System.out.println(Thread.currentThread().getName()+"::"+i+":"+c+"次数");
            }
            c3.signal();  //通知线程3
        }finally {
            lock.unlock();
        }
    }

    public void t15(int c) throws InterruptedException {
        lock.lock();
        try {
            while (flag!=3){

                c3.await();
            }
            flag=1;
            for (int i = 1; i <=15 ; i++) {
                System.out.println(Thread.currentThread().getName()+"::"+i+":"+c+"次数");
            }
            c1.signal();  //通知线程1
        }finally {
            lock.unlock();
        }
    }
  }

public class dingzhihuatongxin {
    public static void main(String[] args) throws InterruptedException {
        Ch ch = new Ch();
        Object o =new Object();
        o.wait();
        new Thread(() -> {
            for (int i = 1; i <=5 ; i++) {
                try {
                    ch.t5(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        },"AA").start();

        new Thread(() -> {
            for (int i = 1; i <=10 ; i++) {
                try {
                    ch.t10(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        },"BB").start();

        new Thread(() -> {
            for (int i = 1; i <=15 ; i++) {
                try {
                    ch.t15(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        },"CC").start();
    }

}
